package com.daxt.controller.notice;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

import com.daxt.common.annotations.LoginAuth;
import com.daxt.common.annotations.LoginUser;
import com.daxt.mapper.sys.ArchivesFondsMapper;
import com.daxt.model.service.archives.vo.ArchivesFonds;
import com.daxt.model.service.base.result.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daxt.common.result.PageInfo;
import com.daxt.common.result.PageUtil;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.archives.ArchivesDocMapper;
import com.daxt.mapper.archives.ArchivesEditResearchMapper;
import com.daxt.model.service.archives.result.ArchivesEditResearchResult;
import com.daxt.model.service.archives.vo.ArchivesClass;
import com.daxt.model.service.archives.vo.ArchivesDoc;
import com.daxt.model.service.archives.vo.ArchivesEditResearch;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "档案编研")
@RestController
public class EditResearchController {
	
	@Autowired
	private ArchivesEditResearchMapper archivesEditResearchMapper;
	@Autowired
	private ArchivesDocMapper archivesDocMapper;
	@Autowired
	private ArchivesFondsMapper archivesFondsMapper;
	@ApiOperation(value = "列表", notes = "")
	@GetMapping("/notice/list/{type}")
	@LoginAuth()
	public Result<PageInfo<ArchivesEditResearchResult>> list(@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize,
			@ApiParam("类型  公告notice  编研archives")@PathVariable("type")String type,
			@ApiParam("全宗id") @RequestParam(value = "fondsId",required=false) String fondsId,
			@ApiIgnore @LoginUser UserInfo loginUser){
		Page<ArchivesEditResearch> page = new Page<>(pageNum == null ? 0 : pageNum,
				pageSize == null ? 10 : pageSize);
		QueryWrapper<ArchivesEditResearch> queryWrapper=new QueryWrapper<ArchivesEditResearch>();
		queryWrapper.eq("atype", type);

		//超级管理员可以看到所有全宗的数据，其它管理员只能看到自己本部门全宗的数据
		String userName= loginUser.getUsername();
		String role = loginUser.getRole();
		if(!"admin".equals(userName) && !"超级管理员".equals(role)){
			String fondsId2=loginUser.getFondsId();
			queryWrapper.eq(StringUtils.isNotEmpty(fondsId2),"fondsId",fondsId2);
		}else{
			queryWrapper.eq(StringUtils.isNotEmpty(fondsId),"fondsId",fondsId);
		}

		IPage<ArchivesEditResearch> info = archivesEditResearchMapper.selectPage(page, queryWrapper);
		List<ArchivesEditResearchResult> list=new ArrayList<ArchivesEditResearchResult>();
		if(info.getRecords().size()>0) {
			for(int i=0;i<info.getRecords().size();i++) {
				ArchivesEditResearch archivesEditResearch = info.getRecords().get(i);
				ArchivesFonds archivesFonds = archivesFondsMapper.selectById(archivesEditResearch.getFondsId());
				if(archivesFonds!=null) archivesEditResearch.setFondsName(archivesFonds.getFondsName());
				ArchivesEditResearchResult result=new ArchivesEditResearchResult();
				BeanUtils.copyProperties(archivesEditResearch, result);
				QueryWrapper<ArchivesDoc> queryWrapperDoc=new QueryWrapper<ArchivesDoc>();
				queryWrapperDoc.eq("id", archivesEditResearch.getDocId());
				Integer count=archivesDocMapper.selectCount(queryWrapperDoc);
				if(count!=null&&count>0) {
					result.setIsexist(1);
				}
				list.add(result);
			}
		}
		
		PageInfo<ArchivesEditResearchResult> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
				list);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, data);
	}
	
	@ApiOperation(value = "发布编研列表", notes = "")
	@GetMapping("/notice/list/release")
	public Result<PageInfo<ArchivesEditResearch>> flist(@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize){
		Page<ArchivesEditResearch> page = new Page<>(pageNum == null ? 0 : pageNum,
				pageSize == null ? 10 : pageSize);
		QueryWrapper<ArchivesEditResearch> queryWrapper=new QueryWrapper<ArchivesEditResearch>();
		queryWrapper.eq("atype", "archives");
		queryWrapper.eq("state", "1");
		IPage<ArchivesEditResearch> info = archivesEditResearchMapper.selectPage(page, queryWrapper);
		PageInfo<ArchivesEditResearch> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
				info.getRecords());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, data);
	}
	
	@ApiOperation(value = "编研/公告详情", notes = "")
	@GetMapping("/notice/{id}")
	public Result<ArchivesEditResearch> details(@PathVariable("id")String id){
		ArchivesEditResearch info = archivesEditResearchMapper.selectById(id);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null,info);
	}
	
	
	@ApiOperation(value = "添加", notes = "")
	@PostMapping("/notice")
	public Result<List<String>> list1(@RequestBody ArchivesEditResearch entity){
		entity.setState("0");
		if(entity.getDocId()==null) {
			entity.setDocId(0L);
		}
		if("archives".equals(entity.getAtype())) {
			entity.getDocId();
			QueryWrapper<ArchivesEditResearch> queryWrapper=new QueryWrapper<ArchivesEditResearch>();
			queryWrapper.eq("docId", entity.getDocId());
			Integer selectCount = archivesEditResearchMapper.selectCount(queryWrapper);
			if(selectCount!=null&&selectCount>0) {
				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"该档案以添加过编研，不可重复添加！！！", null);
			}
		}else if("notice".equals(entity.getAtype())) {
			
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"参数异常", null);
		}
		archivesEditResearchMapper.insert(entity);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "设置发布状态", notes = "")
	@PostMapping("/notice/{state}")
	public Result<List<String>> setState(@PathVariable("state")String state,@RequestBody List<String> ids){
		QueryWrapper<ArchivesEditResearch> updateWrapper=new QueryWrapper<ArchivesEditResearch>();
		updateWrapper.in("id", ids);
		ArchivesEditResearch entity=new ArchivesEditResearch();
		entity.setState(state);
		archivesEditResearchMapper.update(entity, updateWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	
	@ApiOperation(value = "修改", notes = "")
	@PutMapping("/notice")
	public Result<List<ArchivesClass>> edit(@RequestBody ArchivesEditResearch entity){
		if("archives".equals(entity.getAtype())) {
			entity.getDocId();
			QueryWrapper<ArchivesEditResearch> queryWrapper=new QueryWrapper<ArchivesEditResearch>();
			queryWrapper.eq("docId", entity.getDocId());
			queryWrapper.last("id != "+entity.getId());
			Integer selectCount = archivesEditResearchMapper.selectCount(queryWrapper);
			if(selectCount!=null&&selectCount>0) {
				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"该档案以添加过编研，不可重复添加！！！", null);
			}
		}else if("notice".equals(entity.getAtype())) {
			
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"参数异常", null);
		}
		archivesEditResearchMapper.updateById(entity);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "删除", notes = "")
	@DeleteMapping("/notice")
	public Result<List<ArchivesClass>> del(@RequestBody @Valid @NotEmpty List<String> ids){
		archivesEditResearchMapper.deleteBatchIds(ids);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
}
